package com.jwthhealth_pub.old.subscribe;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.LogUtil;
import com.jwthhealth_pub.common.base.BaseActivity;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.api.model.CommonRespone;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;
import com.jwthhealth_pub.old.subscribe.adapter.SubscribeListAdapter;
import com.jwthhealth_pub.old.subscribe.model.SubscribeList;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 我的预约
 * Created by Administrator on 2017/7/18.
 */

public class SubscribeListActivity extends BaseActivity {

    private static final String TAG = "SubscribeListActivity";

    @BindView(R.id.title_layout)
    TitleLayout titleLayout;
    @BindView(R.id.subscribe_list)
    RecyclerView subscribeList;

    private LoginModel.DataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
    private SubscribeListAdapter mListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subscribe_list);
        ButterKnife.bind(this);

//        userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
    }

    private void getData() {
        showLoadProgresbar();
        if (userinfoModel != null) {
            Call<SubscribeList> call = ApiHelper.getAppointmentList(userinfoModel.getId(), userinfoModel.getAndroidtoken());
            call.enqueue(new Callback<SubscribeList>() {
                @Override
                public void onResponse(@NonNull Call<SubscribeList> call, @NonNull Response<SubscribeList> response) {
                    final SubscribeList body = response.body();
                    if (body == null) return;
                    if (body.getCode().equals("0")) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                List<SubscribeList.DataBean> data = body.getData();
                                subscribeList.setLayoutManager(new LinearLayoutManager(SubscribeListActivity.this));
                                mListAdapter = new SubscribeListAdapter(SubscribeListActivity.this, data);
                                subscribeList.setAdapter(mListAdapter);
                                missLoadProgressbar();
                            }
                        });
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                LogUtil.toast(body.getMsg());
                            }
                        });
                    }
                }

                @Override
                public void onFailure(@NonNull Call<SubscribeList> call, @NonNull Throwable t) {
                    LogUtil.e(t.toString(), TAG);
                }
            });
        }
    }

    public void getalertDialog(final boolean isResubscribe, final SubscribeList.DataBean mData) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View dialogView = View.inflate(this, R.layout.subscribe_item_dialog_cancel, null);
        final EditText ed = dialogView.findViewById(R.id.ed);
//        builder.setView(dialogView);

        if (isResubscribe) {
            builder.setMessage(R.string.subscribe_alert_resubscribe);
            builder.setView(null);
        } else {
            builder.setView(dialogView);
            builder.setMessage(R.string.subscribe_alert_cancel);
        }

        builder.setPositiveButton(R.string.subscribe_alert_resubscribe_pos, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                LoginModel.DataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
                if (userinfoModel != null) {
                    Call<CommonRespone> call;
                    if (mData.getAstatus().equals("2")) {
                        String desc = ed.getText().toString().trim();
                        call = ApiHelper.cancelAppoinment(mData.getAid(), desc, userinfoModel.getId(), userinfoModel.getAndroidtoken());
                        ed.setVisibility(View.VISIBLE);
                    } else {
                        call = ApiHelper.cancelAppoinment(mData.getAid(), userinfoModel.getId(), userinfoModel.getAndroidtoken());
                        ed.setVisibility(View.GONE);
                    }


                    call.enqueue(new Callback<CommonRespone>() {
                        @Override
                        public void onResponse(@NonNull Call<CommonRespone> call, @NonNull Response<CommonRespone> response) {
                            final CommonRespone body = response.body();
                            if (body == null) return;
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (isResubscribe) {
                                        //重新预约
//                                        SubscribeListActivity subscribeListActivity = new SubscribeListActivity();
//                                        subscribeListActivity.finish();
//                                        SubscribeActivity subscribeActivity = new SubscribeActivity();
//                                        subscribeActivity.finish();

                                        Intent intent = new Intent(SubscribeListActivity.this, SubscribeActivity.class);
                                        intent.putExtra("subscribe_data", mData);
                                        startActivity(intent);
//                                        finish();
                                    } else {
                                        //已取消，更新页面
                                        if (body.getCode().equals("0")) {
                                            getData();
                                        } else {
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    LogUtil.toast(body.getMsg());
                                                }
                                            });
                                        }
                                    }
                                }
                            });
                        }

                        @Override
                        public void onFailure(@NonNull Call<CommonRespone> call, @NonNull Throwable t) {
                            LogUtil.e(t.toString(), TAG);
                        }
                    });
                }
            }
        });
        builder.setNegativeButton(R.string.subscribe_alert_resubscribe_neg, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        });

        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    public void reSubscibe(final SubscribeList.DataBean mData) {
        LoginModel.DataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
        if (userinfoModel != null) {
            Call<CommonRespone> call = ApiHelper.cancelAppoinment(mData.getAid(), userinfoModel.getId(), userinfoModel.getAndroidtoken());

            call.enqueue(new Callback<CommonRespone>() {
                @Override
                public void onResponse(@NonNull Call<CommonRespone> call, @NonNull Response<CommonRespone> response) {
                    final CommonRespone body = response.body();
                    if (body == null) return;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //重新预约
//                                        SubscribeListActivity subscribeListActivity = new SubscribeListActivity();
//                                        subscribeListActivity.finish();
//                                        SubscribeActivity subscribeActivity = new SubscribeActivity();
//                                        subscribeActivity.finish();

                            Intent intent = new Intent(SubscribeListActivity.this, SubscribeActivity.class);
                            intent.putExtra("subscribe_data", mData);
                            startActivity(intent);
//                                        finish();
                        }
                    });
                }

                @Override
                public void onFailure(@NonNull Call<CommonRespone> call, @NonNull Throwable t) {
                    LogUtil.e(t.toString(), TAG);
                }
            });
        }
    }

    @Override
    protected void title() {
        titleLayout.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    @Override
    protected void onStart() {
//        getData();
        super.onStart();
    }

    @Override
    protected void onResume() {
        getData();
        super.onResume();
    }
}
